# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import json
from jd.model.phone import Phone

class JsonPipeline(object) :
    def open_spider(self, spider) :
        self.file = open('jd.json', 'w')

    def close_spider(self, spider) :
        self.file.close()

    def process_item(self, item, spider) :
        line = json.dumps(dict(item), ensure_ascii=False) + "\n"
        self.file.write(line)
        return item

class JdPipeline(object) :
    def open_spider(self, spider) :
        pass

    def close_spider(self, spider):
        pass

    def process_item(self, item, spider) :
        Phone.insert(item['skuid'], item['category'], item['url'], item['skuname'], item['skushop'], item['detail'], item['comment'])
        return item
